﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using intelliPaper.DB;
using intelliPaper.Model;
using intelliSys.Management.Automation.Test.Properties;
using intelliPaper.FileHandler;

namespace intelliSys.Management.Automation.Test
{
    /// <summary>
    /// Interaction logic for StartPage.xaml
    /// </summary>
    public partial class StartPage : Window
    {
        public StartPage()
        {
            this.InitializeComponent();
            this.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(StartPage_MouseLeftButtonDown);
            this.Loaded += new RoutedEventHandler(StartPage_Loaded);
            // Insert code required on object creation below this point.
        }

        void StartPage_Loaded(object sender, RoutedEventArgs e)
        {
            Settings.Default.ReturnFromException = false;
            LoadAsync();
        }
        
        private void LoadAsync()
        {
            System.Threading.ThreadPool.QueueUserWorkItem(o =>
            {
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (Settings.Default.ReturnFromException)
                    {
                        Message.Show(null, "", MessageType.Wait, "Your application has encountered an error that has resulted in a restart. Please wait while we're trying  recovering your status.", "RECOVER FROM ERROR");
                        ///Do your recovery work;
                    }
                }));
                DataRepository.Load();
                if (Settings.Default.ReturnFromException)
                {
                    System.Threading.Thread.Sleep(5000);
                }
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    MainWindow mw = new MainWindow();
                    if (Settings.Default.ReturnFromException)
                    {
                        try
                        {
                            Settings.Default.ReturnFromException = false;
                            this.Visibility = System.Windows.Visibility.Collapsed ;
                            Message.ShutdownWaitingCurrent();
                            if (Settings.Default.Q != null)
                            {
                                QuestionMgmt qm = new QuestionMgmt();
                                qm.DataContext = Settings.Default.Q;
                                qm.ShowDialog();
                                if (qm.DialogueResult == WindowCommandResult.CreateNewSavingChanges || qm.DialogueResult == WindowCommandResult.OpenSavingChanges)
                                {
                                    Settings.Default.Q.ID = Dao.AddQuestion(Settings.Default.Q);
                                    DataRepository.Questions.Add(Settings.Default.Q);
                                }
                                Settings.Default.Q = null;
                            }

                            else if (Settings.Default.TP != null)
                            {
                                TestPaper tp = new TestPaper();
                                tp.DataContext = Settings.Default.TP;
                                tp.ShowDialog();
                                if (tp.DialogueResult == WindowCommandResult.CreateNewSavingChanges || tp.DialogueResult == WindowCommandResult.OpenSavingChanges)
                                {
                                    Settings.Default.TP.ID = Dao.AddPaper(Settings.Default.TP);
                                    DataRepository.Papers.Add(Settings.Default.TP);
                                }
                                Settings.Default.TP = null;
                            }
                            else if (Settings.Default.KP != null)
                            {
                                TestCriteriaMgmt tmm = new TestCriteriaMgmt();
                                tmm.DataContext = Settings.Default.KP;
                                tmm.ShowDialog();
                                if (tmm.DialogueResult == WindowCommandResult.CreateNewSavingChanges || tmm.DialogueResult == WindowCommandResult.OpenSavingChanges)
                                {
                                    Settings.Default.KP.ID = Dao.AddKnowledgePoint(Settings.Default.KP);
                                    DataRepository.KnowledgePoints.Add(Settings.Default.KP);
                                }
                                Settings.Default.KP = null;
                            }
                            Settings.Default.Save();
                            mw.Show();
                            this.Close();
                        }
                        catch
                        {
                            Settings.Default.ReturnFromException = false;
                            Settings.Default.Q = null;
                            Settings.Default.TP = null;
                            Settings.Default.KP = null;
                            Settings.Default.Save();
                            mw.Show();
                            this.Close();
                            throw new ApplicationException();
                        }
                    }
                    else
                    {
                        Settings.Default.Save();
                        mw.Show();
                        this.Close();
                        if (DataRepository.IsOpenFromFile)
                        {
                            Paper paperInFile = MPDHandler.ReadFromMPDFile(DataRepository.StartFilePath);
                            Helper.FillQuestions(DataRepository.Questions, paperInFile.Questions);
                            TestPaper tp = new TestPaper();
                            tp.DataContext = paperInFile;
                            tp.RefreshStatistics();

                            tp.ShowDialog();
                        }

                    }
                }));
            });
        }

        private void StartPage_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}